package com.helloworld.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.helloworld.pojo.JsonData;
import com.helloworld.pojo.Purchaselist;
import com.helloworld.service.PurchaselistService;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.bcel.internal.generic.RETURN;


@ParentPackage("user")
@Namespace("/")
@Component
public class PurchaselistAction extends ActionSupport {
	
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private PurchaselistService purchaselistService;
	
	private Purchaselist purchaselist;
	
	private Integer pageNum;
	
	private Integer pageSize = 5;
	
	private HttpServletRequest req; 
	
	private JsonData jsonData;
	
	private String returnData;
	
	
	
	
	/**
	 * 分页查询
	 * @return
	 */
	@Action(value="findPurchaselistByPage",results= {@Result(name="list",location="/purchase/purchase-list.jsp")})
	public String findPurchaselistByPage() {
		req=ServletActionContext.getRequest();
		List<Purchaselist> list = new ArrayList<Purchaselist>();
		List<Purchaselist> list2 = new ArrayList<Purchaselist>();
		String time = "";
		if (purchaselist!=null) {
			if (purchaselist.getPurchaseTime()!=null) {
				SimpleDateFormat spFormat = new SimpleDateFormat("yyyy-MM-dd");
				 time = spFormat.format(purchaselist.getPurchaseTime());
			}
		}
		
		if (pageNum==null) {
			   pageNum=1;
		}
		
		if (purchaselist==null) {
			list = purchaselistService.findByPage(null, null,null,null,null,pageNum, pageSize);
			list2 = purchaselistService.findAll();
		}else {
			list = purchaselistService.findByPage(time, purchaselist.getPurchaseId(), purchaselist.getInvoiceNumber(),purchaselist.getSupplierName(),purchaselist.getPurchasePerson(),  pageNum, pageSize);
			list2 = purchaselistService.findByEntity(time,purchaselist.getPurchaseId(), purchaselist.getInvoiceNumber(),purchaselist.getSupplierName(),purchaselist.getPurchasePerson());
		}
		
		
		
		int pages = 0;
		
		if (list2.size()%pageSize==0) {
			pages = list2.size()/pageSize;
		}else {
			pages = (list2.size()/pageSize)+1;
		}
		
		req.setAttribute("purchaselists", list);
		System.out.println("查询到数据:"+list.size());
		req.setAttribute("totalPurchase", list2.size());
		req.setAttribute("pageNum", pageNum);
		req.setAttribute("pageSize", pageSize);
		req.setAttribute("pagePurchase", purchaselist);
		req.setAttribute("pages", pages);
		return "list";
	}
	
	
	
	/**
	 * 添加采购单
	 * @return
	 */
	@Action(value="addPurchase",results= {@Result(name="add_purchase",type="json",params={"root","returnData"})})
	public String addPurchase() {
		jsonData = new JsonData();
		Map<String,String> map=new HashMap<String,String>();
		int result = purchaselistService.addPurchase(purchaselist);
		if(result==0) {
			System.out.println(0);
			map.put("code", "0");
			map.put("msg", "ok");
		}else {
			System.out.println(1);
			map.put("code", "1");
			map.put("msg", "err");
		}
		
		returnData = JSON.toJSONString(map);
		System.out.println();
		return "add_purchase";
	}
	
	
	/**
	 * 添加采购单
	 * @return
	 */
	@Action(value="delPurchase",results= {@Result(name="del_purchase",type="json",params={"root","returnData"})})
	public String delPurchase() {
		jsonData = new JsonData();
		Map<String,String> map=new HashMap<String,String>();
		int result = purchaselistService.delPurchase(purchaselist.getPurchaseId());
		if(result==0) {
			System.out.println(0);
			map.put("code", "0");
			map.put("msg", "ok");
		}else {
			System.out.println(1);
			map.put("code", "1");
			map.put("msg", "err");
		}
		
		returnData = JSON.toJSONString(map);
		System.out.println();
		
		return "del_purchase";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	


	
	

	public String getReturnData() {
		return returnData;
	}



	public void setReturnData(String returnData) {
		this.returnData = returnData;
	}



	public JsonData getJsonData() {
		return jsonData;
	}



	public void setJsonData(JsonData jsonData) {
		this.jsonData = jsonData;
	}



	public Purchaselist getPurchaselist() {
		return purchaselist;
	}


	public void setPurchaselist(Purchaselist purchaselist) {
		this.purchaselist = purchaselist;
	}


	public Integer getPageNum() {
		return pageNum;
	}


	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}


	public Integer getPageSize() {
		return pageSize;
	}


	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	
	
	
	
	

}
